package while_lang.ast;

import java.util.Map;
import java.util.Random;

/** Nodo del AST de While que representa una comparación por igual.
 *  @author Leonardo Val
 */
public class CompEqual extends Exp {
	public final Exp operand1, operand2;
	
	public CompEqual(Exp operand1, Exp operand2) {
		this.operand1 = operand1;
		this.operand2 = operand2;
	}

	@Override public String unparse() { 
		return "("+ operand1.unparse() +" == "+ operand2.unparse() +")";
	}
	
	@Override public void halteadNumbers(Map<String, Integer> operands, Map<String, Integer> operators) {
		operand1.halteadNumbers(operands, operators);
		operand2.halteadNumbers(operands, operators);
		String key = "==";
		Stmt.incrementarDiccionario(key, operators);
	}
	
	public static CompEqual generate(Random random, int min, int max) {
		return new CompEqual(
			Exp.generate(random, min-1, max-1),
			Exp.generate(random, min-1, max-1)
		);
	}
	
	@Override public String toString() {
		return "CompEqual("+ operand1 +", "+ operand2 +")";
	}

	@Override public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result	+ ((operand1 == null) ? 0 : operand1.hashCode());
		result = prime * result + ((operand2 == null) ? 0 : operand2.hashCode());
		return result;
	}

	@Override public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CompEqual other = (CompEqual) obj;
		if (operand1 == null) {
			if (other.operand1 != null)
				return false;
		} else if (!operand1.equals(other.operand1))
			return false;
		if (operand2 == null) {
			if (other.operand2 != null)
				return false;
		} else if (!operand2.equals(other.operand2))
			return false;
		return true;
	}

}
